package com.shan.domm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shan.domm.common.ErrorCode;
import com.shan.domm.exception.BusinessException;
import com.shan.domm.model.domain.Room;
import com.shan.domm.service.RoomService;
import com.shan.domm.mapper.RoomMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 23949
* @description 针对表【room(宿舍)】的数据库操作Service实现
* @createDate 2024-04-10 13:35:11
*/
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
    implements RoomService{


    @Resource
    private RoomMapper roomMapper;

    @Override
    public Boolean addRoom(Room room) {
        return this.save(room);
    }

    @Override
    public Integer deleteRoom(Room room) {
        String dormNumber = room.getDormNumber();
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dorm_number",dormNumber);
        Long l = roomMapper.selectCount(queryWrapper);
        if(l<1){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"没有这个宿舍");
        }
        int delete = roomMapper.delete(queryWrapper);
        return delete;

    }

    @Override
    public Integer updateRoom(Room room) {
        Integer roomId=room.getId();
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",roomId);
        Long l = roomMapper.selectCount(queryWrapper);
        if(l<1){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"没有这个宿舍");
        }
        int i = roomMapper.updateById(room);
        return i;
    }

    @Override
    public Room searchRoom(String dormNumber) {
        if(dormNumber==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dorm_number",dormNumber);
        Room room = roomMapper.selectOne(queryWrapper);
        return room;
    }
}




